<?php
if(!defined('ROOTPATH')) {
    define('ROOTPATH','/var/www/html/new/');
    define('CLASSPATH',ROOTPATH .'d/class/');
    define('COMMONPATH',ROOTPATH .'d/common/');
}
include ROOTPATH  ."com.inc.php";
include COMMONPATH."func.inc.php";
include CLASSPATH ."OrderRecord.php";
include CLASSPATH ."ProOrder.php";
include CLASSPATH ."SoapInit.class.php";
include CLASSPATH ."MemberAccount.class.php";

$action = trim($_REQUEST['action']);
if ($action=='login') {
    $mem = new \pft\Member\MemberAccount($GLOBALS['le']);
    $account  = $_REQUEST['account'];
    $pwd      = $_REQUEST['password'];
    if(!is_numeric($account)) {
        exit ('{"status":"fail", "msg":"登录账号格式错误"}');
    }
    $res = $mem->Login($account, $pwd);
    if($res['status']=='ok') {
        if(!isset($_REQUEST['return_url'])){
            $_SESSION['memberID'] = $res['body']['id'];
            $_SESSION['dname'] = $res['body']['dname'];
            $_SESSION['cname'] = $res['body']['cname'];
            $_SESSION['dtype'] = $res['body']['dtype'];
            if(empty($_COOKIE['account'])) {
                $expire = $_SERVER['REQUEST_TIME']+2592000;//30 days
                setcookie('account',$account, $expire ,'/', 'y.12301.cc');
            }
        }
        else {
            $callBack = $_REQUEST['callback'];
            request_post($_REQUEST['return_url'], array('id'=>$res['body']['id'],'fxid'=>$_REQUEST['fxid']));
            echo $callBack.'({"status":"ok"})';
            exit;
        }
        unset($res['body']);
    }
    echo json_encode($res);
}
elseif ($action=='OrderSearch') {
    ChkLogin();
    include 'order_search.php';
}
elseif ($action=='OrderModify') {
    ChkLogin();
    include 'order_modify.php';
}
elseif($action=='SalerList') {
    ChkLogin();
    $list = SalerList();
    echo json_encode(array('code'=>200,'list'=>$list,'total'=>count($list)));
}

function ChkLogin()
{
    if(!isset($_SESSION['memberID'])){
        exit('{"code":102}');
    }
    if(!in_array($_SESSION['dtype'], array(0,2,9))) {
        exit('{"code":100}');
    }
}

function SalerList()
{
    if($_SESSION['dtype']==2) {
        $mem     = new \pft\Member\MemberAccount($GLOBALS['le']);
        $account = $mem->getAccountById($_SESSION['memberID']);
    }
    $sql = "SELECT salerid,title,getPY(title,1) AS py FROM uu_land WHERE";
    if(isset($_GET['sid'])) {
        $sql .= " id = " . intval($_GET['sid']);
    }
    elseif(isset($account)) {
        $sql .= " salerid={$account} AND status=1";
    }
    else {
        $sql .= " apply_did={$_SESSION['memberID']} AND status=1";
    }
    $sql .= " ORDER BY py ASC";
    $GLOBALS['le']->query($sql);
    $salerids = array();
    while($r = $GLOBALS['le']->fetch_assoc()) {
        $salerids[$r['salerid']] = $r['py'] . '-'.$r['title'];
    }
    return $salerids;
}